//
//  KDServiceViewController.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/9.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDServiceViewController.h"

@interface KDServiceViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView     *_tableView;
    UISegmentedControl  *_segment1;
}


@end

@implementation KDServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setNaviLeftButton];
    [self setNaviTitle:@"我的服务"];
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    _tableView.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
    
    _segment1 = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"否",@"是", nil]];
    [_segment1 setFrame:(CGRect){kScreenWidth-80,18,50,19}];
    [_segment1 addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
}
- (void)viewWillAppear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = YES;
    [self requestGetOpenAdd];
}

- (void)viewWillDisappear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = NO;
}

- (void)change:(UISegmentedControl *)segment
{
    if (segment == _segment1) {
        NSLog(@"seg = %d",_segment1.selectedSegmentIndex);
        [self requestSetOpenAdd];
    }
}

//网络交互
//getOpenAdd
//备注:调取失败
-(void)requestGetOpenAdd
{
    [CSHApiClient getOpenAdddidFinishLoaded:^(KXJson *json, NSString *path) {
        //
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}
//setOpenAdd
- (void)requestSetOpenAdd
{
    [CSHApiClient setOpenAddIs_add:_segment1.selectedSegmentIndex == 0 ? 0 : 1 DidFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            [self showAlert:[json getStringForKey:@"message"]];
        }else{
            [self showAlert:@"操作失败,请重试"];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0)
{
    return NO;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *CustomCellIdentifier = [NSString stringWithFormat:@"CellIdentifier%ld",(long)indexPath.row+indexPath.section*10];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
        [cell.contentView addSubview:_segment1];
        [_segment1 setSelectedSegmentIndex:0];
        UIView  *line = [[UIView alloc] initWithFrame:(CGRect){0,54,WIDTH(cell.contentView),1}];
        [line setBackgroundColor:[UIColor colorWithHexString:@"0xF2F2F2"]];
        [cell.contentView addSubview:line];
    }
    if (indexPath.row == 0) {
        [cell.textLabel setText:@"  是否开通加号服务"];
    }else{
        
    }
    [cell.textLabel setTextColor:[UIColor colorWithHexString:@"666666"]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:15]];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
